package com.sf.rbac.web.controller;

import com.sf.rbac.domain.Employee;
import com.sf.rbac.service.ILoginService;
import com.sf.rbac.util.JsonResult;
import com.sf.rbac.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @GetMapping("/code")
    public JsonResult code() {
        Map<String, String> map = loginService.code();
        return JsonResult.success(map);
    }

    @PostMapping("/login")
    public JsonResult login(@RequestBody LoginVo loginVo) {
        Employee employee = loginService.login(loginVo);
        return JsonResult.success(employee);
    }

    @GetMapping("/logout")
    public JsonResult logout(HttpServletRequest request){
        String userId = request.getHeader("userId");
        loginService.logout(userId);
        return JsonResult.success();
    }
}
